export default function useRouterHook() {
  const goToPage = (url: string, type: string = 'page') => {
    if (type === 'page') {
      uni.navigateTo({ url  })
    } else if (type === 'tab') {
      uni.switchTab({ url })
    }
  }

  const checkIsTab = function(e: any) {
    for (var t = [ "/pages/index/index", "/pages/service/index", "/pages/partner/partner", "/pages/release/index", "/pages/user/user" ], r = !1, n = 0; n < t.length; n++) e.indexOf(t[n]) >= 0 && (r = !0);
    return r;
  }

  const goUrl = (url: string, replace = false) => {
    if (url.substring(0, 1) !== "/") {
      url = "/" + url;
    }

    const navigationMethod = replace ? 'redirectTo' : 'navigateTo';

    if (url.indexOf("http") >= 0) {
      uni[navigationMethod] && uni[navigationMethod]({
        url: "/pages/goUrl?url=" + encodeURIComponent(url),
      });
    } else if (checkIsTab(url)) {
      uni.switchTab({
        url,
      });
    } else {
      uni[navigationMethod] && uni[navigationMethod]({
        url,
      });
    }
  };

  return {
    goToPage,
    goUrl
  }
}
